<?php
/**
 * Created by PhpStorm.
 * User: dingboxu
 * Date: 16/3/11
 * Time: 下午4:30
 */

namespace ActionsHttp;

use Actions\ActionUtils;
use Workerman\Protocols\Http;

class BaseActionHttp
{
    public $dataArr = array();

    public $permit_user_type_list = array(USER_TYPE_SUPER_ADMIN,USER_TYPE_CUSTOMER_SERVICE,USER_TYPE_COMPANY_ADMIN,USER_TYPE_OFFICE_ADMIN);

    public function __construct($event, $requestId, $data, $connection, $isCheckSession = true)
    {
        $this->data = $data;
        $this->connection = $connection;
        $this->event = $event;
        $this->requestId = '0';
        if($requestId)
        {
            $this->requestId = $requestId;
        }
        // 响应码
        $this->resultCode = RESULT_CODE_OK;
        // 错误信息
        $this->errMsg = "";
        $this->dbTrans = null;

//        if ($isCheckSession) {
//
//            if ($_SESSION[SESSION_LOGIN_TIME] && (time() - $_SESSION[SESSION_LOGIN_TIME]) < SESSION_VALIDITY_TIME) {
//                // 有效会话，更新会话时间
//                $_SESSION[SESSION_LOGIN_TIME] = time();
//
//            } else {
//                // 无效会话
//                return $this->SendSessionOut();
//            }
//        }

        $this->doAction();
    }

    public function doAction()
    {

    }



    public function setValue($key,$value)
    {
        $this->dataArr[$key] = $value;
    }



    /**
     * @param $user_type_list
     * @param $company_id
     * @param $office_id
     * @return null
     *
     * 权限检测
     *
     */
//    public function checkPermission($user_type_list, $company_id, $office_id){
//
//        // 管理员类型
//        $user_type = $_SESSION[USER_TYPE];
//
//        // 管理员类型判定
//        if (!in_array($user_type,$user_type_list)) {
//            // 非法操作
//            return false;
//        }
//
//        switch ($user_type) {
//            case USER_TYPE_SUPER_ADMIN:
//                // 超管
//                break;
//            case USER_TYPE_CUSTOMER_SERVICE:
//                // 客服
//                break;
//            case USER_TYPE_COMPANY_ADMIN:
//                // 公司管理员
//                if ($company_id && $_SESSION[COMPANY_ID] != $company_id) {
//                    // 非法操作
//                    return false;
//                }
//                break;
//            case USER_TYPE_OFFICE_ADMIN:
//                // 部门管理员
//                if (($company_id && $_SESSION[COMPANY_ID] != $company_id) || ($office_id && $_SESSION[OFFICE_ID] != $office_id)) {
//                    // 非法操作
//                    return false;
//                }
//                break;
//        }
//
//        // 检测通过
//        return true;
//    }


    /**
     *
     * 成功
     *
     */
    public function ResponseDataToCurrentClient() {
        $this->connection->send(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_OK,$this->errMsg));
    }


    /**
     * @param $errMsg
     * @return null
     *
     * 参数错误
     *
     */
    public function SendErrMsg($errMsg) {
        if ($this->dbTrans) {
            $this->dbTrans->rollBackTrans();
        }
        $this->connection->send(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_PARAM_ERROR,$errMsg));
        return null;
    }


    /**
     * @return null
     *
     * 会话已过期
     *
     */
    public function SendSessionOut() {
        $this->connection->send(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_SESSION_EXPIRED));

        return null;
    }


    /**
     * @return null
     *
     * 非法操作
     *
     */
    public function SendIllegalOperation() {
        $this->connection->send(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_ILLEGAL_OPERATION));
        return null;
    }


    /**
     * @return null
     *
     * 找不到该通讯
     *
     */
    public function SendNotFound() {
        $this->connection->send(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_NOT_FOUND));

        return null;
    }


    /**
     * @return null
     *
     * 服务器内部错误
     *
     */
    public function SendInternalServerError() {
        $this->connection->send(ActionUtils::getResponseData($this->dataArr,$this->event,$this->requestId,RESULT_CODE_INTERNAL_SERVER_ERROR));

        return null;
    }

}